package com.wyx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyx.constans.Constans;
import com.wyx.domain.DictData;
import com.wyx.domain.DictType;
import com.wyx.dto.DictTypeDto;
import com.wyx.mapper.DictDataMapper;
import com.wyx.mapper.DictTypeMapper;
import com.wyx.service.DictTypeService;
import com.wyx.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author xiaoxiong
 * @version 1.0
 * @date 2021/2/25 21:29
 */
@Service
public class DictTypeServiceImpl implements DictTypeService {
    @Autowired
    private DictTypeMapper dictTypeMapper;

    @Autowired
    private DictDataMapper dictDataMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;



    @Override
    public DataGridView listPage(DictTypeDto dictTypeDto) {
        QueryWrapper<DictType> qw = new QueryWrapper<>();
        Page<DictType> page = new Page<>(dictTypeDto.getPageNum(), dictTypeDto.getPageSize());
        // like Mybatisplus 语法(符合条件 ，数据库中字段名，传入的数据)
        qw.like(StringUtils.isNoneBlank(dictTypeDto.getDictName()), DictType.COL_DICT_NAME, dictTypeDto.getDictName());
        qw.like(StringUtils.isNoneBlank(dictTypeDto.getDictType()), DictType.COL_DICT_TYPE, dictTypeDto.getDictType());
        qw.eq(StringUtils.isNoneBlank(dictTypeDto.getStatus()), DictType.COL_STATUS, dictTypeDto.getStatus());
        qw.ge(dictTypeDto.getBeginTime() != null, DictType.COL_CREATE_TIME, dictTypeDto.getBeginTime());
        qw.le(dictTypeDto.getEndTime() != null, DictType.COL_CREATE_TIME, dictTypeDto.getEndTime());
        this.dictTypeMapper.selectPage(page, qw);
        // 返回一个数据传输对象
        DataGridView dataGridView = new DataGridView(page.getTotal(), page.getRecords());
        return dataGridView;
    }

    @Override
    public DataGridView list() {
        QueryWrapper<DictType> qw = new QueryWrapper<>();
        qw.eq(DictType.COL_STATUS, Constans.STATUS_TRUE);
        List<DictType> dictTypes = this.dictTypeMapper.selectList(qw);
        DataGridView dataGridView = new DataGridView(null, dictTypes);
        return dataGridView;
    }


    @Override
    public boolean checkDictTypeUnique(Long dictId, String dictType) {
        dictId = (dictId == null) ? -1L : dictId;
        QueryWrapper<DictType> qw = new QueryWrapper<>();
        qw.eq(DictType.COL_DICT_TYPE, dictType);
        DictType sysDictType = this.dictTypeMapper.selectOne(qw);
        if (sysDictType != null && dictId.longValue() != sysDictType.getDictId().longValue()) {
            // 说明不存在
            return true;
        }
        // 说明存在
        return false;
    }

    @Override
    public int insert(DictTypeDto dictTypeDto) {
        DictType dictType = new DictType();
        // BeanUtil.copyProperties 糊涂工具类 复制一个类到另一个类
        BeanUtil.copyProperties(dictTypeDto, dictType);
        dictType.setCreateTime(DateUtil.date());
        dictType.setCreateBy(dictTypeDto.getSimpleUser().getUserName());
        return this.dictTypeMapper.insert(dictType);
    }

    @Override
    public int update(DictTypeDto dictTypeDto) {
        DictType dictType = new DictType();
        // BeanUtil.copyProperties 糊涂工具类 复制一个类到另一个类
        BeanUtil.copyProperties(dictTypeDto, dictType);
        return this.dictTypeMapper.updateById(dictType);
    }

    @Override
    public int deleteDictTypeByIds(Long[] dictIds) {
        List<Long> longs = Arrays.asList(dictIds);
        if (longs.size() > 0 && longs != null) {
            return this.dictTypeMapper.deleteBatchIds(longs);
        } else {
            return -1;
        }
    }

    @Override
    public DictType selectDictTypeById(Long dictId) {
        if (dictId != null) {
            return this.dictTypeMapper.selectById(dictId);
        }
        return null;
    }

    /**
     * 字典缓存同步
     * 1.查询dictType 中可用的数据
     * 2.根据dicttype 去查询dictdata里面的数据
     * 3.把查询出来的数据 保存到redis
     */
    @Override
    public void dictCacheAsync() {
        QueryWrapper<DictType> qw = new QueryWrapper<>();
        qw.eq(DictType.COL_STATUS, Constans.STATUS_TRUE);
        List<DictType> dictTypes = this.dictTypeMapper.selectList(qw);
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        for (DictType dictType : dictTypes) {
            QueryWrapper<DictData> dqw = new QueryWrapper<>();
            dqw.eq(DictData.COL_STATUS, Constans.STATUS_TRUE);
            dqw.eq(DictData.COL_DICT_TYPE, dictType.getDictType());
            List<DictData> dictData = dictDataMapper.selectList(dqw);
            String key = Constans.DICT_REDIS_PROFIX + dictType.getDictType();
            String value = JSON.toJSONString(dictData);
            valueOperations.set(key,value);
        }
    }
}
